package com.weipai.demo.util;

/**
 * @Auther: gusl
 * @Date: 2019/6/1 17:26
 * @Description:
 */


import org.apache.shiro.web.filter.authc.UserFilter;
import org.apache.shiro.web.servlet.ShiroHttpServletRequest;

import org.springframework.stereotype.Component;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.Cookie;

import javax.servlet.http.HttpSession;


@Component
public class SessionCheckFilter extends UserFilter {


    @Override
    protected boolean onAccessDenied(ServletRequest request, ServletResponse response) throws Exception {
        ShiroHttpServletRequest rrr = (ShiroHttpServletRequest)request;
        String s = rrr.getRequestURI();
        HttpSession session = rrr.getSession();
        Long t1 = session.getCreationTime();

        String userAgent = rrr.getHeader("User-Agent");
        String sid = session.getId();
        String a = rrr.getRequestedSessionId();
        String b = rrr.getQueryString();
        String c = rrr.getPathInfo();
        Cookie d[] = rrr.getCookies();

        //是APP访问
        if (userAgent.indexOf("Android-APP") >= 0 || userAgent.indexOf("IOS-APP") >= 0) {

            response.getWriter().write("{\"code\":\"101\",\"message\":\"token已失效，请重新登录\"}");
            response.getWriter().flush();
            response.getWriter().close();
        }
        else {//是Web访问
            super.redirectToLogin (request, response);
            //super.isLoginRequest(request,response)
        }

        return false;
    }

}
